package com.kadron.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;

import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

import com.kadron.entity.Price;

@AutoCreate
@BypassInterceptors
@Name("priceDAO")
public class KPriceDAO extends KDAO<Price> {

	public Price find(long itemId, long currencyId) {
		EntityManager em = getEntityManager();
		try {			
			return (Price) em.createQuery("SELECT e FROM Price e WHERE e.item.id = :i_id AND e.currency.id = :c_id ")
			.setParameter("i_id", itemId)
			.setParameter("c_id", currencyId)
			.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Price> getPrices(long itemId) {
		EntityManager em = getEntityManager();
		try {			
			return em.createQuery("SELECT e FROM Price e WHERE e.item.id = :item_id ")
			.setParameter("item_id", itemId)
			.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	
}
